package com.cong.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cong.project.common.BaseResponse;
import com.cong.project.common.ErrorCode;
import com.cong.project.common.ResultUtils;
import com.cong.project.exception.BusinessException;
import com.cong.project.model.entity.CommentDetail;
import com.cong.project.model.entity.Content;
import com.cong.project.model.enums.ContentTypeEnum;
import com.cong.project.model.request.*;
import com.cong.project.model.vo.CommentDetailVo;
import com.cong.project.model.vo.PostVo;
import com.cong.project.service.CommentDetailService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 评论控制器
 *
 * @author 86188
 * @date 2023/02/07
 */
@RequestMapping("comment")
@RestController
public class CommentController {

    @Resource
    private CommentDetailService commentDetailService;
    /**
     * 评论列表
     */
    @GetMapping("/list")
    public BaseResponse getCommentDetailList(TokenUserRequest tokenUserRequest, CommentQueryRequest commentQueryRequest) {
        IPage<CommentDetailVo> commentDetailVoIPage =  commentDetailService.getCommentList(tokenUserRequest,commentQueryRequest);
        return ResultUtils.success(commentDetailVoIPage);
    }
    /**
     * 添加评论
     *
     * @param tokenUserRequest 令牌用户请求
     * @param commentAddRequest   post添加请求
     * @return {@link BaseResponse}
     */
    @PostMapping("/add")
    public BaseResponse addCommentDetail(TokenUserRequest tokenUserRequest, @RequestBody CommentAddRequest commentAddRequest){
        if (tokenUserRequest.getId()==null||tokenUserRequest.getId()<=0){
            throw new BusinessException(ErrorCode.TOKEN_ERROR,"非法登录");
        }
        Boolean result = commentDetailService.addCommentDetail(tokenUserRequest,commentAddRequest);
        return ResultUtils.success(result);
    }

    /**
     * 删除评论
     *
     * @param tokenUserRequest 令牌用户请求
     * @param deleteRequest   post添加请求
     * @return {@link BaseResponse}
     */
    @PostMapping("/remove")
    public BaseResponse deleteCommentDetail(TokenUserRequest tokenUserRequest, @RequestBody DeleteRequest deleteRequest){
        if (tokenUserRequest.getId()==null||tokenUserRequest.getId()<=0){
            throw new BusinessException(ErrorCode.TOKEN_ERROR,"非法登录");
        }
        Boolean result = commentDetailService.removeById(deleteRequest.getId());
        return ResultUtils.success(result);
    }

    /**
     * 帖子点赞
     */
    @GetMapping("/detailThumbUp")
    public BaseResponse detailThumbUp(TokenUserRequest tokenUserRequest,Long commentId) {
        if (commentId<=0){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        CommentDetail commentDetail = commentDetailService.getById(commentId);
        if (commentDetail==null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"无此帖子");
        }
        Integer thumbResult =  commentDetailService.thumbUp(tokenUserRequest,commentId, ContentTypeEnum.COMMENT.getCode());
        return ResultUtils.success(thumbResult);
    }

}
